/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.CustomerOrderEntity;
import ejb.CustomerOrderEntityFacade;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author mohan
 */
@WebServlet(name = "ListCustomerOrders", urlPatterns = {"/ListCustomerOrders"})
public class ListCustomerOrders extends HttpServlet {
    @EJB
    private CustomerOrderEntityFacade customerOrderEntityFacade;

    
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
             out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ListCustomerOrders</title>");            
            out.println("</head>");
            out.println("<div align='center'>");
            out.println("<body bgcolor='#CCCCCC'>");
            out.println("<a href = 'Mainindex'>Main Window | </a>");
            out.println("<a href = 'AddCustomer'>Add new customer | </a>");
            out.println("<a href = 'ListCustomers'>View Customers | </a>");
            out.println("<a href = 'AddCustomerOrder'>Add new customer Order | </a>");
            out.println("<a href = 'ListCustomerOrders'>View Customers Orders</a>");
            out.println("<h1>Customer Order Details</h1>");
            List customerOrders = customerOrderEntityFacade.findAll();
            
            out.println("<table width='1000' border='1'>");
            out.println("<tr>");
            out.println("<td><b></b></td>");
            out.println("<td><b>Order No</b></td>");
            out.println("<td><b>Customer ID</b></td>");
            out.println("<td><b>Customer Name</b></td>");
            out.println("<td><b>Amount</b></td>");
            out.println("<td><b>Due Date</b></td>");
            out.println("<td><b>Comments</b></td>");
            out.println("</tr>");
            
            for (Iterator it = customerOrders.iterator(); it.hasNext();) {
                CustomerOrderEntity elem = (CustomerOrderEntity) it.next();
                out.println("<tr>");
                out.println("<td><a href = 'AddCustomerOrder?cusOrderID="+elem.getOrderno()+"'>Update</a></td>");
                out.println("<td><a href = 'ViewCustomerOrder?cusOrderID="+elem.getOrderno()+"'>"+elem.getOrderno()+"</a></td>");
                out.println("<td>"+elem.getCustomerid().getCustomerid()+"</td>");
                out.println("<td>"+elem.getCustomerid().getName()+"</td>");
                out.println("<td>"+elem.getAmount()+"</td>");
                out.println("<td>"+elem.getDueDate()+"</td>");
                out.println("<td>"+elem.getComment()+"</td>");
                out.println("</tr>");
            }
            
            out.println("</table>");
            out.println("<br/>");
            out.println("<a href='AddCustomerOrder'>Add new Customer Order</a>");
            
            out.println("</body>");
            out.println("</div>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
